package hdb.dao;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import hdb.om.Artikel;
import hdb.om.Vorrat;

/**
 * The Data access class for items. All Interaction with the database
 * regarding the entity bean Artikel should be handled by this class!
 * @author juergen.schoenwaelder@stud.fh-deggendorf.de, 
 * 			thomas.eberhart@stud.fh-deggendorf.de
 */
public class ArtikelDao extends HibernateDaoSupport {
	VorratDao vorratDao;

	public void setVorratDao(VorratDao vorratDao) {
		this.vorratDao = vorratDao;
	}
	
	/**
	 * returns a single item by its primary db key
	 * @param id
	 * @return a single Artikel
	 */
	public Artikel findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Artikel) template.get(Artikel.class, id);
	}
	/**
	 * returns a single item by its barcode
	 * @param barcode
	 * @return a single Artikel
	 */
	public List<Artikel> findByBarcode(long barcode) {
		HibernateTemplate template = getHibernateTemplate();
		String hqlString = "from Artikel as art where art.barcode=" + barcode;
		@SuppressWarnings("unchecked")
		List<Artikel> artikel = template.find(hqlString);
		return artikel;
	}
		
	/**
	 * Returns all items from the database.
	 * @return a list of Artikel
	 * @see Artikel
	 */
	@SuppressWarnings("unchecked")
	public List<Artikel> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		
//		SessionFactory hibernateSessionFactory = template.getSessionFactory();
//		Session sess = hibernateSessionFactory.getCurrentSession();
//		SQLQuery sqlQuery = sess.createSQLQuery("SELECT * FROM artikel");
//		List artikel = sqlQuery.list();
		
//		Criteria crit = sess.createCriteria(Artikel.class);
//		List artikel = crit.list();
//		return artikel;
		
		return template.loadAll(Artikel.class);
	}

	/**
	 * modifies an existing item in the database which is selected by its primary key.
	 * @param id the primary key of the item
	 * @param name the new name for the item
	 * @param barcode the new ean of the item.
	 * @param hauptkategorie the main-category of the item (e.g. "drinks")
	 * @param unterkategorie the sub-category of the item (e.g. "juice")
	 * @param geschaeft the store where the item was purchased (e.g. "ALDI")
	 * @param mbestand the number of units where the item must be purchased
	 * @see Artikel
	 */
	public void modify(int id, long barcode, String name, String hauptkategorie, String unterkategorie, String geschaeft, int mbestand) {
		HibernateTemplate template = getHibernateTemplate();
		Artikel artikel = (Artikel) template.get(Artikel.class, id);
		artikel.setBarcode(barcode);
		artikel.setName(name);
		artikel.setHauptkategorie(hauptkategorie);
		artikel.setUnterkategorie(unterkategorie);
		artikel.setGeschaeft(geschaeft);
		artikel.setMbestand(mbestand);
		template.saveOrUpdate(artikel);
	}

	/**
	 * creates a new persistent item in the database providing all the necessary details.
	 * @param barcode the new ean of the item
	 * @param name the name for the new item
	 * @param hauptkategorie the main-category of the item (e.g. "drinks")
	 * @param unterkategorie the sub-category of the item (e.g. "juice")
	 * @param geschaeft the store where the item was purchased (e.g. "ALDI")
	 * @param mbestand the number of units where the item must be purchased
	 * @see Artikel
	 */
	public void create(long barcode, String name, String hauptkategorie, String unterkategorie, String geschaeft, int mbestand) {
		HibernateTemplate template = getHibernateTemplate();
		Artikel artikel = new Artikel(barcode, name, hauptkategorie, unterkategorie, geschaeft, mbestand);
		template.persist(artikel);
	}

	/**
	 * deletes a item using it's id.
	 * @param id
	 * @throws ArtikelDeleteException if there are Vorrat objects involving the item.
	 */
	public void deleteById(int id) throws ArtikelDeleteException {
		HibernateTemplate template = getHibernateTemplate();
		Artikel a = findById(id);
		Set<Vorrat> vorrat = vorratDao.findByArtikel(a);
		if ((vorrat == null) || (vorrat.size() == 0)) {
			template.delete(a);
		} else {
			throw new ArtikelDeleteException();
		}
	}
	
}